<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分组名称" prop="gname">
        <el-input
          v-model="queryParams.gname"
          placeholder="请输入分组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['video:grouping:add']"
        >新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupingList" >
      <el-table-column label="分组名称" align="center" prop="gname" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '1'">启用</span>
          <span v-if="scope.row.status == '2'">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['video:grouping:edit']"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleGoods(scope.row)"
            v-hasPermi="['video:grouping:remove']"
          >管理商品</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['video:grouping:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分组名称" prop="gname">
          <el-input v-model="form.gname" placeholder="请输入分组名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.good_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分组商品管理话框 -->
    <el-dialog :title="title2" :visible.sync="open2" width="1450px" append-to-body>
      <el-form :model="queryParams2" ref="queryForm2" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品分类" prop="cid">
        <el-select v-model="queryParams2.cid" placeholder="请选择商品分类" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in classifyData"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams2.goodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleGoodsQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetGoodsQuery">重置</el-button>
      </el-form-item>
      </el-form>

      <el-table v-loading="loading2" :data="DGoodsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商品ID" align="center" prop="id" />
        <el-table-column label="商品分类" align="center" prop="classifyName" />
        <el-table-column label="商品名称" align="center" prop="goodName" />
<!--        <el-table-column label="商品图片" align="center" prop="imageUrl" />-->
        <el-table-column prop="imageUrl" label="商品图片" width="90"  align="center">
          <template slot-scope="scope">
            　　　    <img :src="scope.row.imageUrl" width="50" height="50" />
          </template>
        </el-table-column>
        <el-table-column label="售价" align="center" prop="sellPrice" />
        <el-table-column label="下载标记" align="center" prop="downloadFlg">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '1'">未下载</span>
            <span v-if="scope.row.status == '2'">已下载</span>
          </template>
        </el-table-column>
        <el-table-column label="试看时间(秒)" align="center" prop="testTime" />
        <el-table-column label="商品状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '1'">启用</span>
            <span v-if="scope.row.status == '2'">禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleGoodsUpdate(scope.row)"
              v-hasPermi="['video:DGoods:edit']"
            >添加到分组</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total2>0"
        :total="total2"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getGoodsList"
      />

      <!--分组明细商品-->
      <el-table v-loading="loading3" :data="GroupItemDtoList" >
        <el-table-column label="商品名称" align="center" prop="goodName" />
<!--        <el-table-column label="商品图片" align="center" prop="imageUrl" />-->
        <el-table-column prop="imageUrl" label="商品图片" width="90"  align="center">
          <template slot-scope="scope">
            　　　    <img :src="scope.row.imageUrl" width="50" height="50" />
          </template>
        </el-table-column>
        <el-table-column label="售价" align="center" prop="sellPrice" />
        <el-table-column label="下载标记" align="center" prop="downloadFlg">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '1'">未下载</span>
            <span v-if="scope.row.status == '2'">已下载</span>
          </template>
        </el-table-column>
        <el-table-column label="试看时间(秒)" align="center" prop="testTime" />
        <el-table-column label="商品状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status == '1'">启用</span>
            <span v-if="scope.row.status == '2'">禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleItemUpdate(scope.row)"
              v-hasPermi="['video:GroupItem:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleGroupItemDelete(scope.row)"
              v-hasPermi="['video:GroupItem:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total3>0"
        :total="total3"
        :page.sync="queryParams3.pageNum"
        :limit.sync="queryParams3.pageSize"
        @pagination="getGroupItemDtoList"
      />
    </el-dialog>

    <!-- 添加分组下商品排序 -->
    <el-dialog :title="title4" :visible.sync="open4" width="500px" append-to-body>
      <el-form ref="form" :model="form4"  label-width="80px">
        <el-input type="hidden" v-model="form4.id" />
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form4.itemsort" placeholder="请输入排序" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitItemForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGrouping, getGrouping, delGrouping, addGrouping, updateGrouping } from "@/api/video/grouping";
import {listDGoods} from "@/api/video/DGoods";
import {listDClassifyVo} from "@/api/video/DClassify";
import {addGroupItem, listGroupItemDto,delGroupItem,updateGroupItem,getGroupItem} from "@/api/video/GroupItem";

export default {
  name: "Grouping",
  dicts: ['good_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      loading2: true,
      loading3: true,
      loading4: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      total3: 0,
      // 分组表格数据
      groupingList: [],
      // 商品表格数据
      DGoodsList: [],
      // 分组明细表格数据
      GroupItemDtoList: [],
      // 弹出层标题
      title: "",
      title2: "",
      title3: "",
      title4: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      open3: false,
      open4: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gname: null,
        gurl: null,
        status: null,
      },
      // 商品列表查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        cid: null,
        goodName: null,
        beginTime: null,
        endTime: null,
      },
      // 商品分组明细查询参数
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
        groupid: null,
        goodid: null,
        goodName: null
      },
      // 表单参数
      form: {},
      form2: {},
      form4: {},
      // 表单校验
      rules: {
      },
      classifyData:[],
      // 日期范围
      dateRange: [],
      groupid: null,
    };
  },
  created() {
    this.getList();
    this.getClassifyList();
    this.getGoodsList();
    this.getGroupItemDtoList();
  },
  methods: {
    /** 查询分组列表 */
    getList() {
      this.loading = true;
      listGrouping(this.queryParams).then(response => {
        this.groupingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**查询分类集合**/
    getClassifyList() {
      listDClassifyVo().then(response => {
        this.classifyData = response.rows;
      });
    },
    //查询商品列表
    getGoodsList(){
      this.loading2 = true;
      console.log(this.dateRange)
      if(this.dateRange.length>0){
        this.queryParams2.beginTime=this.dateRange[0];
        this.queryParams2.endTime=this.dateRange[1];
      }else{
        this.queryParams2.beginTime=null;
        this.queryParams2.endTime=null;
      }
      listDGoods(this.queryParams2).then(response => {
        this.DGoodsList = response.rows;
        this.total2 = response.total;
        this.loading2 = false;
      });
    },

    getGroupItemDtoList(){
      listGroupItemDto(this.queryParams3).then(response => {
        this.GroupItemDtoList = response.rows;
        this.total3 = response.total;
        this.loading3 = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gname: null,
        gurl: null,
        status: "0",
        sort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleGoodsQuery() {
      this.queryParams2.pageNum = 1;
      this.getGoodsList();
    },
    /** 重置按钮操作 */
    resetGoodsQuery() {
      this.dateRange = [];
      this.queryParams2.cid=null;
      this.queryParams2.goodName=null;
      this.queryParams2.beginTime=null;
      this.queryParams2.endTime=null;
      // this.resetForm("queryForm2");
      this.handleGoodsQuery();
    },



    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分组";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGrouping(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分组";
      });
    },
    /** 修改商品分组明细按钮操作 */
    handleItemUpdate(row) {
      const id = row.id || this.ids
      getGroupItem(id).then(response => {
        this.form4 = response.data;
        this.open4 = true;
        this.title4 = "修改分组明细";
      });
    },
    /** 删除按钮操作 */
    handleGroupItemDelete(row) {
      const ids = row.id || this.ids;
      console.log("1111111111"+row.id);
      this.$modal.confirm('是否确认删除分组明细编号为"' + ids + '"的数据项？').then(function() {
        console.log("2222222");
        return delGroupItem(ids);
      }).then(() => {
        this.getGroupItemDtoList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


    /***添加商品到当前分组***/
    handleGoodsUpdate(row) {
      const id = row.id || this.ids
      this.form2= {
        id: null,
        groupid: this.groupid,
        goodid: id,
        itemsort: 1,
      };
      addGroupItem(this.form2).then(response => {
        this.$modal.msgSuccess("新增成功");
      }).then(() => {
        //刷新当前分组下的商品列表
        this.getGroupItemDtoList();
      }).catch(() => {});
    },


    /**管理商品**/
    handleGoods(row) {
      this.reset();
      const id = row.id || this.ids
      const now_gname=row.gname;
      this.groupid=id;
      this.open2 = true;
      this.title2 = "管理分组商品("+now_gname+")";
      // getGrouping(id).then(response => {
      //商品列表查询

       //查询分组下的商品
      this.queryParams3={
        pageNum: 1,
        pageSize: 10,
        groupid: this.groupid,
        goodid: null,
        goodName: null
      },

        listGroupItemDto(this.queryParams3).then(response => {
        this.GroupItemDtoList = response.rows;
        this.total3 = response.total;
          this.loading2 = false;
        this.loading3 = false;
      });




      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGrouping(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrouping(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitItemForm(){
      updateGroupItem(this.form4).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open4 = false;
        this.getGroupItemDtoList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分组名称为"' + row.gname + '"的数据项？').then(function() {
        return delGrouping(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('video/grouping/export', {
        ...this.queryParams
      }, `grouping_${new Date().getTime()}.xlsx`)
    }
  },


  //
};
</script>
